
/***************************************************************************
 *   Copyright (C) 2007 by Alexandre Masselot,,,   *
 *   alex@alex-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "psmnistmspreader.h"

namespace bol {
  boost::regex PSMNistMSPReader::s_re_name("Name:\\s*(\\S+)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_libid("LibId:\\s*(.*)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_precursor("Precursor:\\s*(\\S+)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_status("Status:\\s*(.*)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_fullname("Fullname:\\s*(.*)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_comment("Comment:.*", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_comment_sequence("Comment:.*sequence=(\\S+).*", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_comment_modif("Comment:.*modif=(\\S+).*", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_comment_charge("Comment:.*charge=(\\S+).*", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_parent("Parent:\\s*(.*)", boost::regex_constants::icase|boost::regex_constants::perl);
  boost::regex PSMNistMSPReader::s_re_numpeaks("NumPeaks:\\s*(\\d+)", boost::regex_constants::icase|boost::regex_constants::perl);

  
  PSMNistMSPReader::PSMNistMSPReader()
 : PeptideSpectrumMatchReader()
{
}


PSMNistMSPReader::~PSMNistMSPReader()
{
}

PeptideSpectrumMatch* PSMNistMSPReader::nextMatch(){
  string line;
  boost::cmatch matches;
  PeptideSpectrumMatch *pmatch=NULL;
  SpectrumLcmsms* sp;
  double moz;
  string spdescr;
  while(getline(*inputStream(), line)){
    if(line.c_str()[0] == '#')
      continue;
    if(boost::regex_match(line.c_str(), s_re_name)){
      //@TODO we have to make a choice, between instanciate a new object, a shared pointer, an object which remains still but has to be copied to be kept...
      pmatch = new PeptideSpectrumMatch();
      moz=-1;
      sp=NULL;
      spdescr="";
      continue;
    }
    if(boost::regex_match(line.c_str(), matches, s_re_precursor)){
      string s(matches[1]);
      moz=boost::lexical_cast<double>(string(matches[1]));
      continue;
    }

    if(boost::regex_match(line.c_str(), matches, s_re_parent)){
      if(sp==NULL){
        spdescr=string(matches[1]);
      }else{
        sp->setDescription(matches[1]);
      }
      continue;
    }

    if(boost::regex_match(line.c_str(), matches, s_re_comment)){
      if(moz<0){
        cerr<<"no moz extracted from Precursor line "<<line<<endl;
        exit(1);
      }
      string seq;
      if(boost::regex_match(line.c_str(), matches, s_re_comment_sequence)){
        seq=string(matches[1]);
      }else{
        cerr<<"no sequence info in Comment line "<<line<<endl;
        exit(1);
      }
      if(boost::regex_match(line.c_str(), matches, s_re_comment_modif)){
        pmatch->setPeptide(peptideModifier()->peptide(seq, string(matches[1])));
      }else{
        cerr<<"no modif info in Comment line "<<line<<endl;
        exit(1);
      }
      Pchargemask cmask;
      if(boost::regex_match(line.c_str(), matches, s_re_comment_charge)){
        cmask=Peak::string2chargemask(string(matches[1]));
      }else{
        cerr<<"no charge info in Comment line "<<line<<endl;
        exit(1);
      }
      sp=new SpectrumLcmsms(moz, cmask, 1);
      sp->setDescription(spdescr);
      pmatch->setSpectrum(sp);
      continue;
    }

    if(boost::regex_match(line.c_str(), matches, s_re_numpeaks)){
      int n=boost::lexical_cast<int>(string(matches[1]));
      string line2;
      for(int i=0; i<n; i++){
        if(!getline(*inputStream(), line2)){
          cerr<<"cannot read line "<<(i+1)<<"/"<<n<<endl;
          exit(1);
        }
        istringstream iss(line2);
        double pmoz, pintensity;
        iss>>pmoz>>pintensity;
        pmatch->spectrum()->push_back(new PeakIntensity(pmoz, 0, pintensity));
      }
      SpectrumPeakIntensityUtilities::computeIntensityRank(*pmatch->spectrum());
      sort(pmatch->spectrum()->begin(), pmatch->spectrum()->end(), Peak::ltptr);

      return pmatch;
    }
  }
  return NULL;
  
}

}
